package MyArrayList;

import java.util.Arrays;

public class MyList<T> {

    private int size;
    private  int defaultCapacity = 10;
    private Object[] arr = new Object[defaultCapacity];

    public MyList(int defaultCapacity) {
        this.defaultCapacity = defaultCapacity;
    }

    public MyList() {

    }

    //判断容量是否充足
    private boolean isAmple() {
        if (size >= defaultCapacity) {
            return false;
        }
        return true;
    }

    //数组增容
    private boolean capacity() {
        defaultCapacity = defaultCapacity + (defaultCapacity >>> 1);
        Object[] temp = new Object[defaultCapacity];
        System.arraycopy(arr,0,temp,0,size);
        arr = temp;
        System.out.println("增容成功！！！");
        return true;
    }


    //尾插数据
    public boolean addLast(T data) {
        if ( !isAmple()) {
            capacity();
        }
        arr[size] = data;
        size++;
        System.out.println("尾插成功!!!");
        return true;
    }

    //头插数据
    public boolean addFirst(T data) {
        if ( !isAmple() ) {
            capacity();
        }
        System.arraycopy(arr,0,arr,1,size);
        arr[0] = data;
        size++;
        System.out.println("头插成功!!!");
        return true;
    }

    //按任意位置插入
    public boolean add(int index, T data) {
        if (index < 0 || index > size) {
            throw new IllegalImport("非法输入!!!");
        }

        if ( !isAmple()) {
            capacity();
        }
        int i = size - 1;
        while (i >= index) {
            arr[i+1] = arr[i];
            i--;
        }
        arr[index] = data;
        return true;
    }

    //输出
    @Override
    public String toString() {
        return "MyList{" +
                "arr=" + Arrays.toString(arr) +
                '}';
    }

    //使用 for 循环输出
    public void display() {
        for (int i = 0; i < size ; i++) {
            System.out.print(arr[i]+" ");
        }
    }

    //判断是否为空
    public boolean isEmpty() {
        if (size == 0) {
            return true;
        }
        return false;
    }

    //尾删
    public boolean removeLast() {
        if (isEmpty()) {
            throw new NullPointer("尾删除失败!!!");
        }

        size--;
        return true;
    }

    //头删
    public boolean removeFirst() {
        if (isEmpty()) {
            throw new NullPointer("头删除失败!!!");
        }

        int i = 0;
        while ( i < size) {
            arr[i] = arr[i+1];
            i++;
        }
        size--;

        return true;
    }


    //按任意位置删除数据
    public boolean remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalImport("非法输入!!!");
        }
        if (isEmpty()) {
            throw new NullPointer("nullArray!!!");
        }

        int i = index;
        while (i + 1 < size) {
            arr[i] = arr[i+1];
            i++;
        }
        size--;
        return true;
    }

    //当前的数据个数
    public int sum() {
        return size;
    }

    // 判定某个元素的下标
    public int  indexOf(T data) {
        for (int i = 0; i < size; i++) {
            if (data.equals(arr[i])) {
                return i;
            }
        }
        return -1;
    }

    //判断是否存在元素
    public boolean contain(T data) {
        if (indexOf(data) == -1) {
            return false;
        }
        return true;
    }

    // 获取 pos 位置的元素
    public T get(int i) {
        if (isEmpty()) {
            throw new NullPointer("nullArray!!!");
        }
        if (i < 0 || i >= size) {
            throw new IllegalImport("输入非法!!!");
        }
        return (T) arr[i];
    }

    // 给 pos 位置的元素设为【更新为】 value
    public boolean set(int pos, T value) {
        if (isEmpty()) {
            throw new NullPointer("nullArray!!!");
        }
        if (pos < 0 || pos >= size) {
            throw new IllegalImport("输入非法!!!");
        }
        arr[pos] = value;
        return true;
    }

    //清空数组
    public void clear() {
        for (int i = 0; i < size; i++) {
            arr[i] = null;
            size = 0;
        }
    }
}
